//
//  CQWelcomeViewController.swift
//  weibo
//
//  Created by chengqiang on 16/7/22.
//  Copyright © 2016年 chengqiangBiaodashi. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class CQWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        // Do any additional setup after loading the view.
        self.setIcon()
        CQUserViewModel.shareduserAccountViewModel.loadUserInfo(){(error)->() in
            if error != nil {
                print("获取用户信息失败")
                return

            }
            
        }
    }
    
    private func setIcon(){
        if let avatar_large = CQUserViewModel.shareduserAccountViewModel.userAccount?.avatar_large{
            let url = NSURL(string: avatar_large)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    //头像动画
    private func startAnimation(){
        let offset = -(UIScreen.mainScreen().bounds.height-160)
        iconView.snp_updateConstraints { (make)-> Void in
            make.bottom.equalTo(self.view).offset(offset)
        }
        UIView.animateWithDuration(1, delay: 0.2, usingSpringWithDamping: 0.75, initialSpringVelocity: 7, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
        }) { (_) -> Void in
            // 动画完成
            print("动画完成")
            UIView.animateWithDuration(DefaultAnimtionDuration, animations: { () -> Void in
                self.welcomeLable.alpha = 1
                }, completion:{(_)->Void in
                    AppDelegate.switchRootViewController(CQMianController())
            })
        }

    }
    private func setupUI(){
        
        self.view.addSubview(backImageView)
        self.view.addSubview(iconView)
        self.view.addSubview(welcomeLable)
        
        
        backImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        iconView.snp_makeConstraints { (make)-> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        welcomeLable.snp_makeConstraints { (make)->Void  in
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
    private lazy var backImageView:UIImageView=UIImageView(image: UIImage(named: "ad_background"))
    
    
    private  lazy var iconView:UIImageView =
    {
  
        let image = UIImageView(image: UIImage(named: "avatar_default_big"))
        image.layer.cornerRadius=42.5
        image.layer.masksToBounds = true
        image.contentMode = UIViewContentMode.ScaleAspectFit
        
        return image
    }()

    private lazy var welcomeLable:UILabel = {
        let lable = UILabel()
        
        lable.text="欢迎回来"
        lable.font=UIFont.systemFontOfSize(18)
        
        lable.sizeToFit()
        lable.alpha=0

        return lable
    
    }()

}
